<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * admin.php
 * @author ranuandra
 *
 * 25 Apr 2012 15:16:09
 *
 * 
 */
abstract class Kohana_Admin extends Controller_Template

{
	
	public $main_template = '';
	
	public $main_right = '';
	
	public $banner = '';
	public $right = '';
	
	public $template = 'admin/index';
      // Controls access for the whole controller, if not set to FALSE we will only allow user roles specified
      // Can be set to a string or an array, for example array('login', 'admin') or 'login'
    public $auth_required = FALSE;
 
      // Controls access for separate actions
      // 'adminpanel' => 'admin' will only allow users with the role admin to access action_adminpanel
      // 'moderatorpanel' => array('login', 'moderator') will only allow users with the roles login and moderator to access action_moderatorpanel
    public $secure_actions = FALSE;
    
    public $action;
    
    public $directory;
    
    public $limit = 20;
	
/**
      * Initialize properties before running the controller methods (actions),
      * so they are available to our action.
      */
/**
      * Initialize properties before running the controller methods (actions),
      * so they are available to our action.
      */
    
     public function before()
      {
		 parent::before();
      	 //$this->main_right = View::factory('right/index');
      	 //$this->main_right->registrasi = View::factory('right/register');
      	 //$this->template->right = $this->main_right;
         
         $this->action = Request::current()->action();
         //echo Request::current()->controller();
         $action_name = Request::current()->action();

         if (($this->auth_required !== FALSE && Auth_ORM::instance()->logged_in($this->auth_required) === FALSE) || (is_array($this->secure_actions) && array_key_exists($action_name, $this->secure_actions)))
		 {
		 	
			if (Auth_ORM::instance()->logged_in()){
				Request::current()->redirect(URL::base()."admin/hrd");
				
				/**if(Member::instance()->get_user()->activated == 0)
				{
					Request::instance()->redirect(URL::base().'register/activated/'.Member::instance()->get_user()->id);

				}
				else
				{
					if(Member::instance()->get_user()->verified == 0)
					{
						Request::instance()->redirect(URL::base().'member/detail');
					}					
				}**/
			}
			else 
			{

				Request::current()->redirect(URL::base()."admin/home");
			}
		 }
		 
		 

         if($this->auto_render)
          {
            // Initialize empty values
            $this->template->title            = '';
            $this->template->meta_keywords    = '';
            $this->template->meta_description = '';
            $this->template->meta_copywrite   = '';
            $this->template->header           = '';
            $this->template->content          = '';
            $this->template->footer           = '';
            $this->template->styles           = array();
            $this->template->scripts          = array();
            $this->template->main			  = '';
          }
      }

     /**
      * Fill in default values for our properties before rendering the output.
      */
     public function after()
      {
         if(Request::current()->is_ajax() == $this->request)
		 {
		 	$this->auto_render = false;
		 	$this->template->test          = true;
		 }	
		 else
		 {
		 	$this->auto_render = true;
		 	//$this->template->test           = "";
		 }
		 
         if($this->auto_render)
          {
             // Define defaults
             $styles                  = array(URL::base().'assets/css/reset.css' => 'screen');
             $scripts                 = array(URL::base().'assets/js/jquery-1.4.2.min.js');

             // Add defaults to template variables.
             $this->template->styles  = array_reverse(array_merge($this->template->styles, $styles));
             $this->template->scripts = array_reverse(array_merge($this->template->scripts, $scripts));
             $this->template->ref = "";
            
           }
           
           if($this->banner!="")
           {
           		$this->template->banner = $this->banner;
           }
           
           if($this->right!="")
           {
           		$this->template->right = $this->right;
           }
         

           $this->template->menu = View::factory('admin/menu/index');
      	    $user = Auth_ORM::instance()->get_user();
	        if($user)
	        {
	        	$roles = $user->roles->find_all()->as_array('id', 'name');
	        	if(in_array('admin', $roles))
	        	{
	        		$this->template->menu->role = 'admin';
	        	}   
	         	else if (in_array('cabang', $roles))
	        	{
	        		$this->template->menu->role = 'cabang';
	        	}   
	        }
         // Run anything that needs to run after this.
         parent::after();
      } 
      
      public function logs($activity = "")
      {
 			$logs = ORM::factory('log');
			$logs->user_id = Auth_ORM::instance()->get_user();
			$logs->tgl = date("Y-m-d H:m:s");
			$logs->activity = $activity;
			$logs->ip = Request::$client_ip;
			$logs->save();     	
      }
}
?>